package com.fulihui.wgj.dal.mybatis.repository;

import com.fulihui.wgj.dal.mybatis.domain.UserLogin;
import com.fulihui.wgj.dal.mybatis.domain.UserLoginWx;
import com.fulihui.wgj.dal.mybatis.mapper.UserLoginWxMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author wang_wx
 * @date 2018-06-20
 */
@Component
public class UserLoginRepository extends BaseRepository<UserLogin> {

    @Resource
    UserLoginWxMapper userLoginWxMapper;

    /**
     * 用户绑定公众号
     *
     * @param loginId
     * @param appIds
     * @return
     */
    @Transactional(value = "mysqlTransactionManager", rollbackFor = Exception.class)
    public int bindUserWxPlatform(String loginId, List<String> appIds) {
        UserLoginWx userLoginWx = new UserLoginWx();
        userLoginWx.setLoginId(loginId);
        int delete = userLoginWxMapper.delete(userLoginWx);
        if (CollectionUtils.isEmpty(appIds)) {
            return delete;
        }
        int insert = userLoginWxMapper.insertUserLoginWx(loginId, appIds);
        return insert;
    }
}
